{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**468. Validate IP Address**  <font color=#f0ad4e size=3>_medium_</font>\n",
    "\n",
    "Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.\n",
    "\n",
    "**IPv4** addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots (\".\"), e.g.,```172.16.254.1```;\n",
    "\n",
    "Besides, leading zeros in the IPv4 is invalid. For example, the address ```172.16.254.01``` is invalid.\n",
    "\n",
    "**IPv6** addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (\":\"). For example, the address ```2001:0db8:85a3:0000:0000:8a2e:0370:7334``` is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so ```2001:db8:85a3:0:0:8A2E:0370:7334``` is also a valid IPv6 address(Omit leading zeros and using upper cases).\n",
    "\n",
    "However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, ```2001:0db8:85a3::8A2E:0370:7334``` is an invalid IPv6 address.\n",
    "\n",
    "Besides, extra leading zeros in the IPv6 is also invalid. For example, the address ```02001:0db8:85a3:0000:0000:8a2e:0370:7334``` is invalid.\n",
    "\n",
    "**Note:** You may assume there is no extra space or special characters in the input string.\n",
    "\n",
    "**Example 1**\n",
    "\n",
    "```\n",
    "Input: \"172.16.254.1\"\n",
    "\n",
    "Output: \"IPv4\"\n",
    "\n",
    "Explanation: This is a valid IPv4 address, return \"IPv4\".\n",
    "```\n",
    "\n",
    "**Example 2**\n",
    "\n",
    "```\n",
    "Input: \"2001:0db8:85a3:0:0:8A2E:0370:7334\"\n",
    "\n",
    "Output: \"IPv6\"\n",
    "\n",
    "Explanation: This is a valid IPv6 address, return \"IPv6\".\n",
    "```\n",
    "\n",
    "**Example 3**\n",
    "\n",
    "```\n",
    "Input: \"256.256.256.256\"\n",
    "\n",
    "Output: \"Neither\"\n",
    "\n",
    "Explanation: This is neither a IPv4 address nor a IPv6 address.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IPv4\n",
      "IPv6\n",
      "Neither\n",
      "Neither\n"
     ]
    }
   ],
   "source": [
    "def validIPAddress(IP):\n",
    "    \"\"\"\n",
    "    :type IP: str\n",
    "    :rtype: str\n",
    "    \"\"\"\n",
    "    # hex_set = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E']\n",
    "    # ip4_arr = IP.split('.')\n",
    "    # ip6_arr = IP.split(':')\n",
    "    #\n",
    "    # if ip4_arr and len(ip4_arr) == 4:\n",
    "    #     for d in ip4_arr:\n",
    "    #         if d.isdigit() and int (d) <= 255 and int (d) >= 0:\n",
    "    #             continue\n",
    "    #         else:\n",
    "    #             return 'Neither'\n",
    "    #     return 'IPv4'\n",
    "    # elif ip6_arr and len(ip6_arr) == 8:\n",
    "    #     for chars in ip6_arr:\n",
    "    #         if chars == '0':\n",
    "    #             continue\n",
    "    #         if chars:\n",
    "    #             for c in chars:\n",
    "    #                 if c.upper() in hex_set:\n",
    "    #                     continue\n",
    "    #                 else:\n",
    "    #                     return 'Neither'\n",
    "    #         else:\n",
    "    #             return 'Neither'\n",
    "    #     return 'IPv6'\n",
    "    # else:\n",
    "    #     return 'Neither'\n",
    "\n",
    "    def isIPv4(s):\n",
    "        try:return str(int(s)) == s and 0 <= int(s) <= 255\n",
    "        except:False\n",
    "    def isIPv6(s):\n",
    "        if len(s) > 4: return False\n",
    "        try:return int(s, 16) >= 0 and s[0] != '-'\n",
    "        except:False\n",
    "\n",
    "    if IP.count('.') == 3 and all(isIPv4(i) for i in IP.split('.')):\n",
    "        return 'IPv4'\n",
    "    if IP.count(':') == 7 and all(isIPv6(i) for i in IP.split(':')):\n",
    "        return 'IPv6'\n",
    "    return 'Neither'\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(validIPAddress('172.16.254.1'))\n",
    "    print(validIPAddress('2001:0db8:85a3:0:0:8A2E:0370:7334'))\n",
    "    print(validIPAddress('256.256.256.256'))\n",
    "    print(validIPAddress('2001:0db8:85a3:0::8A2E:0370:7334'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
